package com.yuanzong.wuliu.config.system;

import com.yuanzong.wuliu.common.exception.BusinessException;
import com.yuanzong.wuliu.common.vo.ApiResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class ExceptionHandler {

    /**
     * 业务异常全局处理
     */
    @org.springframework.web.bind.annotation.ExceptionHandler(BusinessException.class)
    public ApiResult handleBusinessException(BusinessException e) {
        return ApiResult.errMsg(e.getErrorMsg());
    }

    /**
     * 方法参数校验
     */
    @org.springframework.web.bind.annotation.ExceptionHandler(MethodArgumentNotValidException.class)
    public ApiResult handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        return ApiResult.errMsg(e.getBindingResult().getFieldError().getDefaultMessage());
    }

    /**
     * 系统异常全局处理
     */
    @org.springframework.web.bind.annotation.ExceptionHandler(Exception.class)
    public ApiResult handleException(Exception e) {
        log.error("系统发生错误", e);
        e.printStackTrace();
        return ApiResult.errMsg("系统繁忙,请稍后重试");
    }

}
